package org.csc.sss.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.csc.sss.admin.controller.base.BaseController;
import org.csc.sss.datacore.annontation.WebLog;
import org.csc.sss.datacore.dto.PersonDTO;
import org.csc.sss.datacore.primary.model.Person;
import org.csc.sss.datacore.secondary.model.SystemConfig;
import org.csc.sss.util.Result;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("测试Controller")
@RestController
@RequestMapping(value = "api/v1/system/person")
public class TestController extends BaseController{

	@ApiOperation("创建一个人")
	@WebLog("创建一个人")
	@PostMapping(value = "/")
	public Result<?> addPerson(PersonDTO dto) {

		Person person = super.personRepositoryImpl.save(dto);
		if (null == person) {
			return super.error(Result.ResultCode.INSERT_DATA_ERROR);
		}

		return super.ok();
	}

	@ApiOperation("根据id修改一个人")
	@WebLog("根据id修改一个人")
	@PutMapping(value = "/{id}")
	public Result<?> updatePerson(@PathVariable String id, PersonDTO dto) {

		dto.setId(id);
		if (!super.personRepositoryImpl.update(dto)) {
			return super.error(Result.ResultCode.UPDATE_DATA_ERROR);
		}

		return super.ok();
	}

	@ApiOperation("根据id删除一个人")
	@WebLog("根据id删除一个人")
	@DeleteMapping(value = "/{id}")
	public Result<?> deletePerson(@PathVariable String id) {

		super.personRepositoryImpl.delete(id);

		return super.ok();
	}

	@ApiOperation("根据id查询一个人")
	@WebLog("根据id查询一个人")
	@GetMapping(value = "/{id}")
	public Result<Person> getPerson(@PathVariable String id) {
		return super.ok(super.personRepository.findOne(id));
	}

	@ApiOperation("查询人列表-分页")
	@WebLog("查询人列表-分页")
	@GetMapping(value = "/page")
	public Result<Page<Person>> listPagePerson(PersonDTO dto, @ApiParam("页数") Integer page, @ApiParam("每页大小") Integer seiz, @ApiParam("排序字段") String sortField){

		return super.ok(super.personRepository.findAll(super.personRepositoryImpl.personSpecification(dto), super.pageRequest(page, seiz, sortField, true)));
	}

	@ApiOperation("查询人列表")
	@WebLog("查询人列表")
	@GetMapping(value = "/")
	public Result<List<Person>> listPerson(PersonDTO dto) {
//		Person p = super.personRepository.findOne("402880e6632dec3801632dec8cb90000");
		dto.setId("3");
		return super.ok(super.personRepository.findAll(super.personRepositoryImpl.personSpecification(dto)));
	}

	@GetMapping(value = "/listSystemConfig")
	public Result<List<SystemConfig>> listSystemConfig() {
		return super.ok(super.systemConfigRepository.findAll());
	}
}
